﻿using Ledict.Core.Data;
using Ledict.Utility.Helper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.Common
{
    /// <summary>
    /// 对sql语句的通用操作，包含通用方法与ifreesql对象
    /// </summary>
    public static class SqlManage
    {
        /// <summary>
        ///构建条款项库Ifreesql
        /// </summary>
        static Lazy<IFreeSql> lawMachineLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
        .UseMonitorCommand(cmd =>
        Console.WriteLine($"sql:{cmd.CommandText}")
        )
        .UseNoneCommandParameter(true)
        .UseConnectionString(FreeSql.DataType.SqlServer, ConfigHelper.ConnectionStrings("LawMachine").ConnectionString)
        .Build()
        );

        /// <summary>
        ///构建DocSystem库Ifreesql
        /// </summary>
        static Lazy<IFreeSql> JudgeDocSystemLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
        .UseMonitorCommand(cmd =>
        Console.WriteLine($"sql:{cmd.CommandText}")
        )
        .UseNoneCommandParameter(true)
        .UseConnectionString(FreeSql.DataType.SqlServer, ConfigHelper.ConnectionStrings("JudgeDoc").ConnectionString)
        .Build()
        );

        /// <summary>
        ///构建DocSystem库Ifreesql
        /// </summary>
        static Lazy<IFreeSql> CommonManageLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
        .UseMonitorCommand(cmd =>
        Console.WriteLine($"sql:{cmd.CommandText}")
        )
        .UseNoneCommandParameter(true)
        .UseConnectionString(FreeSql.DataType.SqlServer, ConfigHelper.ConnectionStrings("LdtCommonManage").ConnectionString)
        .Build()
        );
        /// <summary>
        /// 查询条款项库Ifreesql
        /// </summary>
        public static IFreeSql sqlServerLawMachine => lawMachineLazy.Value;

        /// <summary>
        /// 查询DocSystem库Ifreesql
        /// </summary>
        public static IFreeSql sqlServerJudgeDocSystem => JudgeDocSystemLazy.Value;

        /// <summary>
        /// 查询CommonManage库Ifreesql
        /// </summary>
        public static IFreeSql sqlServerCommonManage => CommonManageLazy.Value;

        /// <summary>
        /// 根据数据库和sql语句查询datatable
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public static DataTable GetDataTableBySqlCondition(string sql, string dbName)
        {
            try
            {
                Database oBase = DatabaseFactory.CreateDatabase(dbName);
                var table = oBase.ExecuteDataSet(CommandType.Text, sql).Tables[0];
                if (table.Rows.Count > 0)
                {
                    return table;
                }
                return new DataTable();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("查询表出错：" + ex.Message);
                return new DataTable();
            }
        }

        /// <summary>
        /// 执行受影响的行数返回结果
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public static bool ExecuteNonQuerySql(string sql, string dbName)
        {
            try
            {
                Database oBase = DatabaseFactory.CreateDatabase(dbName);
                if (oBase.ExecuteNonQuery(CommandType.Text, sql) > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("执行ExecuteNonQuery出错：" + ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 执行返回首行首列的值
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public static string ExecuteScalarSql(string sql, string dbName)
        {
            try
            {
                Database oBase = DatabaseFactory.CreateDatabase(dbName);
                var obj = oBase.ExecuteScalar(CommandType.Text, sql);
                if (obj != null)
                    return Convert.ToString(obj);
                else
                    return string.Empty;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("执行ExecuteScalarSql出错：" + ex.Message);
                return string.Empty;
            }
        }
    }
}

